#include "Cello.h"


var get_args(int argc, char** argv) {
  var args = new(Array, String);
  for (int i = 0; i < argc; i++) {
    push(args, new(String, $S(argv[i])));
  }
  return args;
}

var shift(var xs) {
  var x = copy(get(xs, $I(0)));
  pop_at(xs, $I(0));
  return x;
}


var read_entire_file(var file_path) {
  var content = new(String);
  with(f in new(File, file_path, $S("rb"))) {
    sseek(f, 0, SEEK_END);
    int64_t size = stell(f);
    resize(content, size);
    sseek(f, 0, SEEK_SET);
    sread(f, c_str(content), size);
  }
  return content;
}

int main(int argc, char** argv) {
// %() allocate value on stack 
// new allocate value on heap
  var args = get_args(argc, argv);
  var program_name = shift(args);
  if (len(args) <= 0) {
    println("Usage: %$ <FILES...>", program_name);
    println("ERROR: No files were provided");
    return 1;
  }
  foreach (file_path in args) {
    var content = read_entire_file(file_path);
    print("%s", content);
  }
  return 0;
}
